//
// Created by tcj on 2022/4/1.
//

#ifndef __SYLAR_TCP_SERVER_H__
#define __SYLAR_TCP_SERVER_H__

#include "memory"
#include "functional"
#include "address.h"
#include "iomanager.h"
#include "socket.h"
#include "noncopyable.h"

namespace sylar{
    class TcpServer : public std::enable_shared_from_this<TcpServer> , Noncopyable{
    public:
        typedef std::shared_ptr<TcpServer> ptr;

        TcpServer(sylar::IOManager* woker = sylar::IOManager::GetThis(),
                    sylar::IOManager* accpet_woker = sylar::IOManager::GetThis());
        virtual ~TcpServer();

        virtual bool bind(sylar::Adress::ptr addr);
        virtual bool bind(const std::vector<Adress::ptr>& addrs,std::vector<Adress::ptr>& fails);
        virtual bool start();
        virtual void stop();

        uint64_t  getReadTimeout() const {return m_recvTimeout;}
        std::string getName() const {return m_name;}
        void setReadTimeout(uint64_t v){m_recvTimeout =v;}
        void setName(const std::string& v) {m_name =v;}

        bool isStop() const {return m_isStop;}

    protected:
        virtual void handleClient(Socket::ptr client);
        virtual void startAccept(Socket::ptr sock);

    private:
        std::vector<sylar::Socket::ptr> m_socks;
        sylar::IOManager* m_worker;
        sylar::IOManager* m_Acceptworker;
        uint64_t m_recvTimeout;
        std::string m_name;
        bool m_isStop;

    };
}


#endif //SERVER_FRAMEWORK_TCP_SERVER_H
